package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"gitlab-sync/config"
	"gitlab-sync/gitlabapi"
	"strconv"
)

var (
	projectSyncCmd = &cobra.Command{
		Use:   "project",
		Short: "项目同步 $  gitlab-sync project -d={all}/{projectname}",
		Long: `项目同步命令:

  gitlab-sync project -d=all  --同步所有项目
  gitlab-sync project -d=all -p=1 -s=20 --同步第1页的20个项目
  gitlab-sync project -d={projectname} --同步指定项目

`,
		Run: func(cmd *cobra.Command, args []string) {
			data := cmd.Flag("d")
			page := cmd.Flag("page")
			size := cmd.Flag("size")
			var cpage = "1"
			var prepage = config.GetConfig().Gitlab.ProjectPrepage
			if page != nil {
				cpage = page.Value.String()
			}
			if size != nil {
				prepage = size.Value.String()
			}
			if data != nil {
				//按项目名搜索时，默认第一页
				if data.Value.String() != "all" {
					cpage = "1"
					prepage = config.GetConfig().Gitlab.ProjectPrepage
				}
				cpage, _ := strconv.Atoi(cpage)
				gitlabapi.CreateProjects(data.Value.String(), prepage, cpage)
			} else {
				fmt.Println("命令错误，请查看： gitlab-sync project --help")
			}
		},
	}
)

func init() {
	projectSyncCmd.Flags().StringP("d", "d", "", "同步类型")
	projectSyncCmd.Flags().StringP("page", "p", "", "第几页")
	projectSyncCmd.Flags().StringP("size", "s", "", "每页条数")
	rootCmd.AddCommand(projectSyncCmd)
}
